//
//  MainSetViewController.m
//  DaleTou
//
//  Created by wenrong on 2018/5/28.
//  Copyright © 2018年 geren. All rights reserved.
//

#import "MainSetViewController.h"
#import "ShareValue.h"
#import "MBProgressHUD+MJ.h"

@interface MainSetViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    float dataNum;
}
@property (weak, nonatomic) IBOutlet UITableView *tbView;
@property (weak, nonatomic) IBOutlet UIButton *exitLogin;

@property(nonatomic, strong) NSArray *datasource;
@property(nonatomic, strong) NSArray *imgs;
@end

@implementation MainSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor mainLightGrayColor];
    _datasource = @[@"修改绑定手机", @"修改登录密码", @"清除缓存"];
    _imgs = @[@"手机号", @"密码", @"缓存"];
    self.tbView.tableFooterView = [UIView new];
    self.tbView.rowHeight = 58;
}
- (IBAction)exitLoginEvent:(id)sender
{
    [ShareValue sharedInstance].isLogin = NO;
    [ShareValue sharedInstance].phoneNum = nil;
    [ShareValue sharedInstance].password = nil;
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"image"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
    });
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    dataNum = [self getRandomNumber:1 to:100] / 10.0;
    self.exitLogin.hidden = ![ShareValue sharedInstance].isLogin;
}

- (int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from + 1)));
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _datasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    if (indexPath.row != 2) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    } else {
        cell.detailTextLabel.textColor = [UIColor lightGrayColor];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.1fM",dataNum];
    }
    NSString *title = _datasource[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:_imgs[indexPath.row]];
    cell.textLabel.text = title;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *title = _datasource[indexPath.row];
    if ([title isEqualToString:@"修改登录密码"]) {
        [self performSegueWithIdentifier:@"setPushChangePwd" sender:nil];
    } else if ([title isEqualToString:@"修改绑定手机"]) {
        [self performSegueWithIdentifier:@"SetPushChangePhoneNumber" sender:nil];
    } else {
        [MBProgressHUD showMessage:@"正在清除缓存" toView:self.view];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:self.view];
            dataNum = 0;
            [self.tbView reloadData];
        });
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.2];
    return view;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
